//
//	@author: Trương Hoàng Linh
//	Khi người dùng nhấn Bắt đầu (gọi hàm handleKeyDown) sẽ chuyển sang màn tiếp theo (mainScene)
//	Còn thiếu: -hiển số level, hiện info, button tắt mở nhạc, vv.vv
//
var instance_welcome;
(function (window){
	function WelcomeScene (stage, contentManager, gameWidth, gameHeight) {
		this.WelcomeSceneStage = stage;
		this.WelcomeSceneContentManager = contentManager;
		this.gameHeight = gameHeight;
		this.gameWidth = gameWidth; 

		this.WelcomeSceneContainer = new Container();


		instance_welcome = this;
		this.WelcomeSceneContainer.mouseEnabled = true; 
		this.WelcomeSceneContainer.onPress = function(event) {
				instance_welcome.handleKeyDown();
		}


		//Initial all of resources for this class
		this.mainImg 		= this.load(this.WelcomeSceneContentManager.bg_main, this.gameWidth/2, this.gameHeight/2);
		this.btnPlayImg 	= this.load(this.WelcomeSceneContentManager.btn_play, -200, 270);

		this.topGameImg 	= this.load(this.WelcomeSceneContentManager.top_game, 630, 95);
		this.pressedTop = false;
		this.topGameImg.addEventListener("mousedown", function() {
			if (!instance_welcome.pressedTop) {
				instance_welcome.pressedTop = true;

				createjs.Tween.get(instance_welcome.bannerImg,{loop:true}).to({scaleX:1.1, scaleY: 1.1}, 600).to({scaleX:0.9, scaleY: 0.9}, 800);
				 window.getTopPlayer();
			}
		});
		
		this.bannerImg 		= this.load(this.WelcomeSceneContentManager.banner, this.gameWidth/2, this.gameHeight/2);
		this.bannerImg.scaleX = 0;
		this.bannerImg.scaleY = 0;
		this.bannerImg.alpha  = 0;
		
		this.btnPlayMusic	= this.load(this.WelcomeSceneContentManager.btn_music_on, -200, 400);
		this.btnStopMusic	= this.load(this.WelcomeSceneContentManager.btn_music_off, 80, 400); this.btnStopMusic.visible = false;


		this.textCurrentRuby= this.createText(savescore.getRuby(), 665, 16); this.textCurrentRuby.textAlign = 'center';
		this.bg_ruby		= this.load(this.WelcomeSceneContentManager.bg_ruby, 700, 35, 0.9, 0.9);
	
		this.textCurrentLevel = this.createText(savescore.getUserLevel(), 535, 16); this.textCurrentLevel.textAlign = 'center';

		this.setActionForButton();
	}

	WelcomeScene.prototype.addStage = function() {
		//add container vao stage
		this.WelcomeSceneStage.addChild(this.WelcomeSceneContainer);
		
		this.textCurrentLevel.text = savescore.getUserLevel();
		this.textCurrentRuby.text = savescore.getRuby();

		this.runEffect();
	}
	
	
	WelcomeScene.prototype.runEffect = function() {
		//chay hieu ung
		createjs.Tween.get(this.bannerImg).to({scaleX:1, scaleY: 1, alpha: 1}, 500);
		createjs.Tween.get(this.btnPlayImg).wait(300).to({x:80}, 500);
		createjs.Tween.get(this.btnPlayMusic).wait(300).to({x:80}, 500);
	}

	WelcomeScene.prototype.setActionForButton = function() {
		this.press1down = false
		this.btnPlayImg.addEventListener("mousedown", instance_welcome.mouseDownButtonPlay);
		this.btnPlayImg.addEventListener("mouseover", instance_welcome.mouseOverButtonPlay);
		this.btnPlayImg.addEventListener("mouseout", instance_welcome.mouseOutButtonPlay);
		
		this.btnPlayMusic.addEventListener("mousedown", instance_welcome.mouseDownMusicOn);
		this.btnPlayMusic.addEventListener("mouseover", instance_welcome.mouseOverMusicOn);
		this.btnPlayMusic.addEventListener("mouseout", instance_welcome.mouseOutMusicOn);
		
		this.btnStopMusic.addEventListener("mousedown", instance_welcome.mouseDownMusicOff);
		this.btnStopMusic.addEventListener("mouseover", instance_welcome.mouseOverMusicOff);
		this.btnStopMusic.addEventListener("mouseout", instance_welcome.mouseOutMusicOff);
	}



	WelcomeScene.prototype.load = function (data, x, y, scaleX, scaleY) {
		var bitmap = new createjs.Bitmap(data);
		var width = bitmap.image.width;
		var height = bitmap.image.height;
		bitmap.x = x || 0;
		bitmap.y = y || 0;
		bitmap.regX = width/2;
		bitmap.regY = height/2;
		bitmap.scaleX = scaleX || 1;
		bitmap.scaleY = scaleY || 1;
		bitmap.width = width*bitmap.scaleX;
		bitmap.height = height*bitmap.scaleY;
		this.WelcomeSceneContainer.addChild(bitmap);
		return bitmap;
	}



	WelcomeScene.prototype.createText = function(text, x, y, size, bold, color) {
		if (bold === undefined)
			bold = "bold";
		else
			bold = "";

		if (color === undefined )
			color = '#000';
		
		if (size === undefined)
			size = '30';

		var text = new Text(text, bold + ' ' + size +'px Arial', color);
		text.x = x;
		text.y = y;
		this.WelcomeSceneContainer.addChild(text);
		return text;
	}


	WelcomeScene.prototype.getTopPlayer = function(data) {
		//xoa banner
		instance_welcome.bannerImg.visible = false;
		var self = instance_welcome;
		data  = data.replace(/(\r\n|\n|\r)/gm,"");
		var arrPlayer  = data.split("|");

		self.playerNameData = new Array();
		self.playerScoreData = new Array();

		for (var i = 0; i < (arrPlayer.length - 1); i++) {
			var player = arrPlayer[i].split("-");

			self.playerNameData.push(player[0].trim());
			self.playerScoreData.push(player[1].trim());
		}

		self.showTopPlayer();
	}


	WelcomeScene.prototype.showTopPlayer = function() {
		this.title = this.createText("Top Cao Thủ ĐHBC 2014", this.gameWidth/2 - 50, 95, 20, undefined, '#fff'); this.title.textAlign = 'center';

		var yStart = 140;
		this.playerNameShow = new Array();
		this.playerScoreShow = new Array();
		for (var i = 0; i < this.playerNameData.length; i++) {
			 if (i < 3) {
			 	var bold = undefined;
			 	var color = '#fbff3f';
			 } else {
			 	var bold = "1";
			 	var color = '#fff';
			 }
			 this.playerNameShow[i] = this.createText((i+1)+'. ' +this.playerNameData[i], 140, yStart, 18, bold, color);
			 this.playerScoreShow[i] = this.createText(this.playerScoreData[i], 500, yStart, 18, bold, color);
			 if (yStart > 435) {
			 	this.playerNameShow[i].visible = false;
			 	this.playerScoreShow[i].visible = false;
			 }
			yStart += 25;
		}


		var shapeLine = new createjs.Shape();
		shapeLine.graphics.beginFill("#000");
		shapeLine.alpha = 0.7;
		shapeLine.graphics.drawRect(570, 150, 3, 280);
		
		this.xButtonTop = 566;
		this.yButtonTop = 186;
		this.yMaxButtonTop = 391;
		this.buttonTopPlayer = new createjs.Shape();
		this.buttonTopPlayer.graphics.beginStroke("#fff");
		this.buttonTopPlayer.graphics.setStrokeStyle(1); this.buttonTopPlayer.alpha = 0.7;
		this.buttonTopPlayer.graphics.beginFill("#fff");
		this.buttonTopPlayer.graphics.drawRect(0, 0, 10, 76);
		this.buttonTopPlayer.snapToPixel = true;
		this.buttonTopPlayer.regY = 76/2;
		this.buttonTopPlayer.x = this.xButtonTop;
		this.buttonTopPlayer.y = this.yButtonTop;
		this.buttonTopPlayer.shadow = new createjs.Shadow("#ddd", 0, 0, 10);

		this.WelcomeSceneContainer.addChild(shapeLine, this.buttonTopPlayer);


		this.buttonTopPlayer.addEventListener("pressmove", instance_welcome.mousePressedTop);
		this.buttonTopPlayer.addEventListener("pressup", instance_welcome.mousePressedUpTop);


		//nen che tren va duoi danh sach
		var shapeTop = new createjs.Shape();
 		shapeTop.graphics.beginFill("#B92D16").drawRect(0, 0, 440, 24);
 		shapeTop.x = 130;
 		shapeTop.y = 117;
 		var shapeBottom = new createjs.Shape();
 		shapeBottom.graphics.beginFill("#B92D16").drawRect(0, 0, 440, 24);
 		shapeBottom.x = 130;
 		shapeBottom.y = 432;
 		this.WelcomeSceneContainer.addChild(shapeTop, shapeBottom);
	}

	WelcomeScene.prototype.mousePressedTop = function(e) {
		var self = instance_welcome;
		var distance = e.stageY  - self.buttonTopPlayer.y;
		self.buttonTopPlayer.y += distance;
		if (self.buttonTopPlayer.y < self.yButtonTop) { self.buttonTopPlayer.y = self.yButtonTop; return;}
		if (self.buttonTopPlayer.y > self.yMaxButtonTop) { self.buttonTopPlayer.y = self.yMaxButtonTop; return; }
		self.moveList(distance);
	}

	WelcomeScene.prototype.moveList = function(distance) {
		for (var i = 0; i < 20; i++) {
			this.playerScoreShow[i].y -= distance;
			this.playerNameShow[i].y = this.playerScoreShow[i].y;
			if (this.playerScoreShow[i].y > 435 || this.playerScoreShow[i].y < 123 ) {
				this.playerScoreShow[i].visible = this.playerNameShow[i].visible = false;
			} else {
				this.playerScoreShow[i].visible = this.playerNameShow[i].visible = true;
			}
		}
	}


	WelcomeScene.prototype.mousePressedUpTop = function() {

	}



	WelcomeScene.prototype.mouseDownButtonPlay = function() {
		var self = instance_welcome;
		if (!self.press1down) {
			self.press1down = true;
			self.DoWhenPressButton();
		}
	};


	WelcomeScene.prototype.DoWhenPressButton = function() {
		var self = instance_welcome;
		if ( !window.isReady ) {
			if ( !isTextLoad ) {
				isTextLoad = true;
				self.WelcomeSceneStage.removeAllChildren();
				self.WelcomeSceneStage.addChild(window.loading);
			}
			window.setTimeout(self.DoWhenPressButton, 100);
		} else {
			isTextLoad = false;
			this.press1down = false;
			self.WelcomeSceneStage.removeAllChildren();
			ingame.initialize();
			self.WelcomeSceneStage.addChild(ingame);
		}
	}


	WelcomeScene.prototype.mouseOverButtonPlay = function() {
		var self = instance_welcome;
		createjs.Tween.get(self.btnPlayImg).to({scaleX: self.btnPlayImg.scaleX + 0.1,scaleY:self.btnPlayImg.scaleY + 0.1},200);
	};


	WelcomeScene.prototype.mouseOutButtonPlay = function() {
		var self = instance_welcome;
		createjs.Tween.get(self.btnPlayImg).to({scaleX: self.btnPlayImg.scaleX - 0.1,scaleY:self.btnPlayImg.scaleY - 0.1},100);	
	};
	
	
	WelcomeScene.prototype.mouseDownMusicOn = function() {
		console.log("Tat nhac");
		var self = instance_welcome;
		self.btnPlayMusic.visible = false;
		self.btnStopMusic.visible = true;
	}
	WelcomeScene.prototype.mouseOverMusicOn = function() {
		var self = instance_welcome;
		createjs.Tween.get(self.btnPlayMusic).to({scaleX: self.btnPlayMusic.scaleX + 0.1,scaleY:self.btnPlayMusic.scaleY + 0.1},200);
	}
	WelcomeScene.prototype.mouseOutMusicOn = function() {
		var self = instance_welcome;
		createjs.Tween.get(self.btnPlayMusic).to({scaleX: self.btnPlayMusic.scaleX - 0.1,scaleY:self.btnPlayMusic.scaleY - 0.1},200);
	}
		
	WelcomeScene.prototype.mouseDownMusicOff = function() {
		var self = instance_welcome;
		console.log("Bat nhac");
		self.btnPlayMusic.visible = true;
		self.btnStopMusic.visible = false;	
	}
	WelcomeScene.prototype.mouseOverMusicOff = function() {
		var self = instance_welcome;
		createjs.Tween.get(self.btnStopMusic).to({scaleX: self.btnStopMusic.scaleX + 0.1,scaleY:self.btnStopMusic.scaleY + 0.1},200);
	}
	WelcomeScene.prototype.mouseOutMusicOff = function() {
		var self = instance_welcome;
		createjs.Tween.get(self.btnStopMusic).to({scaleX: self.btnStopMusic.scaleX - 0.1,scaleY:self.btnStopMusic.scaleY - 0.1},200);
	}

	window.WelcomeScene = WelcomeScene;
}(window));
